home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / SpreadSheet / InputField.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  2.3 KB  |  95 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4.  
  5. class InputField {
  6.    int maxchars = 50;
  7.    int cursorPos;
  8.    Applet app;
  9.    String sval;
  10.    char[] buffer;
  11.    int nChars;
  12.    int width;
  13.    int height;
  14.    Color bgColor;
  15.    Color fgColor;
  16.  
  17.    public InputField(String initValue, Applet app, int width, int height, Color bgColor, Color fgColor) {
  18.       this.width = width;
  19.       this.height = height;
  20.       this.bgColor = bgColor;
  21.       this.fgColor = fgColor;
  22.       this.app = app;
  23.       this.buffer = new char[this.maxchars];
  24.       this.nChars = 0;
  25.       if (initValue != null) {
  26.          initValue.getChars(0, initValue.length(), this.buffer, 0);
  27.          this.nChars = initValue.length();
  28.       }
  29.  
  30.       this.sval = initValue;
  31.    }
  32.  
  33.    public void setText(String val) {
  34.       for(int i = 0; i < this.maxchars; ++i) {
  35.          this.buffer[i] = 0;
  36.       }
  37.  
  38.       this.sval = new String(val);
  39.       if (val == null) {
  40.          this.sval = "";
  41.          this.nChars = 0;
  42.          this.buffer[0] = 0;
  43.       } else {
  44.          this.sval.getChars(0, this.sval.length(), this.buffer, 0);
  45.          this.nChars = val.length();
  46.          this.sval = new String(this.buffer);
  47.       }
  48.    }
  49.  
  50.    public String getValue() {
  51.       return this.sval;
  52.    }
  53.  
  54.    public void paint(Graphics g, int x, int y) {
  55.       g.setColor(this.bgColor);
  56.       g.fillRect(x, y, this.width, this.height);
  57.       if (this.sval != null) {
  58.          g.setColor(this.fgColor);
  59.          g.drawString(this.sval, x, y + this.height / 2 + 3);
  60.       }
  61.  
  62.    }
  63.  
  64.    public void mouseUp(int x, int y) {
  65.    }
  66.  
  67.    public void keyDown(int key) {
  68.       if (this.nChars < this.maxchars) {
  69.          switch (key) {
  70.             case 8:
  71.                --this.nChars;
  72.                if (this.nChars < 0) {
  73.                   this.nChars = 0;
  74.                }
  75.  
  76.                this.buffer[this.nChars] = 0;
  77.                this.sval = new String(new String(this.buffer));
  78.                break;
  79.             case 9:
  80.             default:
  81.                this.buffer[this.nChars++] = (char)key;
  82.                this.sval = new String(new String(this.buffer));
  83.                break;
  84.             case 10:
  85.                this.selected();
  86.          }
  87.       }
  88.  
  89.       this.app.repaint();
  90.    }
  91.  
  92.    public void selected() {
  93.    }
  94. }
  95.